angular.module('dayactivitycontrollers',[])
  //当天活动
  .controller('DayActivityCtrl',function($scope,$ionicActionSheet,Meeting,Loading,Calendar,Storage,History,ENV,Toast,ShareServiceOther,ShareService){
    var memberId = Storage.get('member').member_id;
    var cityId = '';
    var cityName = '';
    $scope.picPath = ENV.picPath;
    $scope.isShareShow = ShareService.getIsShowShareWindow();//判断是否显示分享框
    $scope.weekDay = [];
    $scope.meetingList = [];
    var dateTime = Storage.get("dateTime");
    $scope.nowDate = new Date(dateTime);//获取当选择的日期
    Loading.showLoading();
    $scope.$on('$ionicView.beforeEnter', function () {
      if(Storage.get("lbsCity")&&Storage.get("lbsCity")!=null && Storage.get("lbsCity")!=''){
        var lbsCity = Storage.get("lbsCity");
        cityName=lbsCity.city_name;
        cityId = lbsCity.shop_city_id;
      }else{
        cityName = '广州市';
      }
      if($scope.meetingList!=undefined && $scope.meetingList.length>0){
        searchList = [];
        for(i=0;i<$scope.meetingList.length;i++){
          search = {
            mm_id:$scope.meetingList[i].mm_id,
            member_id:memberId
          };
          searchList.push(search);
        }
        if(searchList.length>0){
          Meeting.getCurrentMeetingInfo(searchList);
        }
      }
      /*//控制是否显示分享按键
      $scope.showShare = ShareService.getIsShowShareWindow();*/
    });
    $scope.$on('Meeting.getCurrentMeetingInfoUpdated',function(){
      var result = Meeting.getCurrentInfo();
      for(i=0;i<$scope.meetingList.length;i++){
        for(j=0;j<result.length;j++){
          if($scope.meetingList[i].mm_id==result[j].mm_id){
            $scope.meetingList[i].mm_comment_num = result[j].mm_comment_num;
            $scope.meetingList[i].lmeetStatus = result[j].lmeetStatus;
            $scope.meetingList[i].mcmeetStatus = result[j].mcmeetStatus;
          }
        }
      }
    });
    //点击前一周
    $scope.preWeek = function(dayStr){
      var curDate =  new Date(dayStr);
      var preDate = new Date(curDate.getTime() - 24*60*60*1000);  //前一天
      $scope.nowDate = preDate;
      $scope.weekDay =  getWeekDay(preDate);
      Calendar.getCalendar($scope.weekDay,'dayActivity',memberId,cityId,cityName);
      Meeting.getActivityByDay(memberId,formatDate(preDate),cityId,cityName);
    };
    //点击下一周
    $scope.nextWeek = function(dayStr){
      var curDate =  new Date(dayStr);
      var nextDate = new Date(curDate.getTime() + 24*60*60*1000);  //后一天
      $scope.nowDate = nextDate;
      $scope.weekDay =  getWeekDay(nextDate);
      Calendar.getCalendar($scope.weekDay,'dayActivity',memberId,cityId,cityName);
      Meeting.getActivityByDay(memberId,formatDate(nextDate),cityId,cityName);
    };

    $scope.$on('Calendar.getDayActivityCalendarUpdated',function(){
      $scope.weekDay = Calendar.getWeekDayJson();
    });
    $scope.$on('Meeting.activityUpdated', function(){
      $scope.meetingList = Meeting.getActivityListJson().meetingList;
      Loading.hideLoading();
    });

    $scope.selectDay = function(timestr){
      $scope.nowDate = new Date(timestr);
      for(i=0;i<$scope.weekDay.length;i++){
        if(timestr==$scope.weekDay[i].timeformat){
          $scope.weekDay[i].selected = true;
        }else{
          $scope.weekDay[i].selected = false;
        }
      }
      //根据具体时间获取活动
      //Loading.showLoading();
      Meeting.getActivityByDay(memberId,timestr,cityId,cityName);
    };

    var collectIndex = '';//收藏的索引
    var likeIndex = '';//点赞的索引
    //收藏
    $scope.collectMeeting = function(index,mmId){
      Loading.showLoading();
      collectIndex = index;
      Meeting.collectMeeting(memberId,mmId);
    };
    //网络访问成功后
    $scope.$on('Meeting.collectMeetingUpdated', function(){
      Loading.hideLoading();
      var result = Meeting.getCollectJson();
      $scope.meetingList[collectIndex].mcmeetStatus = result.mcmeetStatus;
      Toast.showMessage(result.mcmeetMsg);
      collectIndex='';
    });
    //点赞
    $scope.likeMeeting = function(index,mmId){
      Loading.showLoading();
      likeIndex = index;
      Meeting.likeMeeting(memberId,mmId);
    };
    //网络访问成功后
    $scope.$on('Meeting.likeMeetingUpdated', function(){
      Loading.hideLoading();
      var result = Meeting.getLikeJson();
      $scope.meetingList[likeIndex].lmeetStatus = result.lmeetStatus;
      Toast.showMessage(result.lmeetMsg);
      likeIndex='';
    });

    $scope.goback = function(){
      History.goback();
    };


    /* ========================分享约局按钮  begin======================= */
    //显示约局分享框
    $scope.showMeetingShare = function(mm_id){
      ShareServiceOther.showShareWindow(mm_id,"meeting");
    };
    /* =========================分享约局按钮  end =============================== */




  });
